#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
int val[6]={1,10,100,1000,10000};
string s;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>s;
		for(char &c:s) c-='A';
		ll ans=-2e9;
		auto check=[&](int p){
			if(p==string::npos) return;
			for(char i:{0,1,2,3,4})
			{
				swap(i,s[p]);
				char mc=0;
				ll sum=0;
				for(int j=s.size()-1;j>=0;j--)
				{
					if(s[j]>=mc)
					{
						sum+=val[s[j]];
						mc=s[j];
					}
					else sum-=val[s[j]];
				}
				swap(i,s[p]);
				ans=max(ans,sum);
			}
	
        };
		for(int i:{0,1,2,3,4} )
		{
			check(s.find_first_of(i));
			check(s.find_last_of(i));
		}
		cout<<ans<<endl;
	}

	return 0;
}

